package systemviews;

import java.util.Vector;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.DefaultListCellRenderer;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListModel;
import com.sun.lwuit.util.Log;

import umacis.core.hasForm;
import umacis.core.Umacis;
import umacis.data.elements.Binding;
import umacis.data.elements.Key;
import umacis.data.view.ApplicationView;
import umacis.data.view.SystemView;
import umacis.implementation.ActionHook;

public class OutlineView implements hasForm {
	private List list;
	Form form;
	
	public OutlineView() {
		new Binding(new int[] {Key.DELETE, Key.NORMAL}, new ActionHook("Remove component") {  public void  x() { remove(); }} );
		new Binding(new int[] {Key.LSK, Key.NORMAL}, new ActionHook("Edit properties") {  public void  x() { editProperties(); }} );
		
	}

	protected void editProperties() {
	}

	protected void remove() {
	}

	public void setup() {
		ApplicationView applicationView = Umacis.getInstance().getApplication().getCurrentApplicationView();
		DefaultListModel listModel = new DefaultListModel();
		Vector components = new Vector();
		listComponents(components, listModel, "", applicationView.getForm().getContentPane());
		list.setModel(listModel);
		
	}
	
	private Vector listComponents(Vector allComponents, ListModel outlineMenu,String indentation, Container container) {
		Component[] components = new Component[container.getComponentCount()];
		Log.p("Component count: " + String.valueOf(container.getComponentCount()), Log.INFO);
		
		for (int i=0;i<components.length;i++) {
			Component c = container.getComponentAt(i);
			allComponents.addElement(c);
			outlineMenu.addItem(indentation + c.getUIID());
			if (Container.class.isInstance(c)) listComponents(allComponents, outlineMenu, indentation + ".",(Container)c);
		}
		return allComponents;
	}
	
	public Form getForm() {
		if (form == null) {
			list = new List();
			list.setListCellRenderer(new DefaultListCellRenderer(false));
			list.setSmoothScrolling(true);		
			form = new Form();
			form.setLayout(new BorderLayout());
			form.addComponent(BorderLayout.CENTER,list);
			form.addCommand(new Command("Cancel"));
			form.addCommand(new Command("Edit"));
			form.setTitle("Outline");
		}
		setup();
		return form;
	}

	
}
